﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Obra.SmugMug.Api;
using Obra.Api;
using NUnit.Framework;
using NMock2;

namespace Obra.Test.SmugMug
{
    [TestFixture]
    public class AlbumTester : 
        NMockTester
    {
        private ISmugMugClient smugMugClient;

        private ISession session;

        private int albumId = 1;
        private int sessionId = 2;

        public override void SetUp()
        {
            base.SetUp();
            this.smugMugClient = this.Mockery.NewMock<ISmugMugClient>();
            
            this.session = this.Mockery.NewMock<ISession>();
        }

        private void ExpectOnce(
            string method,
            object value)
        {
            Expect.Once.On(this.smugMugClient)
                .Method(method)
                .With(this.sessionId.ToString(), this.albumId, value);
        }

        [Test]
        public void Test()
        {
            Expect.AtLeast(1).On(this.session)
                .GetProperty("SessionId")
                .Will(Return.Value(this.sessionId.ToString()));

            CategoryContract categoryContract = new CategoryContract()
            {
                Id = 1,
                Name = "category",
            };

            CommunityContract communityContract = new CommunityContract()
            {
                Id = 1,
            };

            HighlightContract highlightContract = new HighlightContract()
            {
                Id = 1,
            };

            SubCategoryContract subCategoryContract = new SubCategoryContract()
            {
                Id = 1,
                Name = 1,
            };

            TemplateContract templateContract = new TemplateContract()
            {
                Id = 1,
            };

            WatermarkContract watermarkContract = new WatermarkContract()
            {
                Id = 1,
            };

            AlbumContract albumContract = new AlbumContract()
            {
                Backprinting = "backprinting",
                CanRank = true,
                Category = categoryContract,
                Clean = true,
                ColorCorrection = 1,
                Comments = true,
                Community = communityContract,
                DefaultColor = true,
                Description = "description",
                Exif = true,
                External = true,
                FamilyEdit = true,
                FileNames = true,
                FriendEdit = true,
                Geography = true,
                Header = true,
                HideOwner = true,
                Highlight = highlightContract,
                Id = this.albumId,
                // ImageContractMap
                ImageCount = 0,
                Key = "key",
                Keywords = "keywords",
                Larges = true,
                LastUpdatedString = "2000-01-01 00:00:00",
                //Media = new List<IMedia>(),
                Originals = true,
                Password = "password",
                PasswordHint = "password hint",
                Position = 1,
                Printable = true,
                ProofDays = 1,
                Protected = true,
                Public = true,
                Share = true,
                SmugSearchable = true,
                //SortDirection = SortDirection.Ascending,
                SortDirectionSerializedXml = true,
                SortMethod = SortMethod.DateTime,
                SubCategory = subCategoryContract,
                // Tags
                Template = templateContract,
                Title = "title",
                UnsharpAmount = 1.0f,
                UnsharpRadius = 1.0f,
                UnsharpSigma = 1.0f,
                UnsharpThreshold = 1.0f,
                Watermark = watermarkContract,
                Watermarking = true,
                WorldSearchable = true,
                X2Larges = true,
                X3Larges = true,
                XLarges = true,
            };

            Album album = new Album(
                albumContract,
                this.session,
                this.smugMugClient);

            // Read
            Assert.AreEqual("backprinting", album.Backprinting);
            Assert.AreEqual(true, album.CanRank);
            Assert.AreEqual("category", album.Category.Name);
            Assert.AreEqual(true, album.Clean);
            Assert.AreEqual(1, album.ColorCorrection);
            Assert.AreEqual(true, album.Comments);
            Assert.AreEqual(1, album.Community.Id);
            Assert.AreEqual(true, album.DefaultColor);
            Assert.AreEqual("description", album.Description);
            Assert.AreEqual(true, album.Exif);
            Assert.AreEqual(true, album.External);
            Assert.AreEqual(true, album.FamilyEdit);
            Assert.AreEqual(true, album.FileNames);
            Assert.AreEqual(true, album.FriendEdit);
            Assert.AreEqual(true, album.Geography);
            Assert.AreEqual(true, album.Header);
            Assert.AreEqual(true, album.HideOwner);
            Assert.AreEqual(this.albumId, album.Highlight.Id);
            Assert.AreEqual(this.albumId, album.Id);
            Assert.AreEqual(0, album.ImageCount);
            Assert.AreEqual("key", album.Key);
            Assert.AreEqual(true, album.Larges);
            Assert.AreEqual(2000, album.LastModified.Year);
            Assert.AreEqual("title", album.Name);
            Assert.AreEqual(true, album.Originals);
            Assert.AreEqual("password", album.Password);
            Assert.AreEqual("password hint", album.PasswordHint);
            Assert.AreEqual(1, album.Position);
            Assert.AreEqual(true, album.Printable);
            Assert.AreEqual(1, album.ProofDays);
            Assert.AreEqual(true, album.Protected);
            Assert.AreEqual(true, album.Public);
            Assert.AreEqual(true, album.Share);
            Assert.AreEqual(true, album.SmugSearchable);
            Assert.AreEqual(SortDirection.Descending, album.SortDirection);
            Assert.AreEqual(SortMethod.DateTime, album.SortMethod);
            Assert.AreEqual(1, album.SubCategory.Name);
            Assert.AreEqual(1, album.Template.Id);
            Assert.AreEqual(1.0, album.UnsharpAmount);
            Assert.AreEqual(1.0, album.UnsharpRadius);
            Assert.AreEqual(1.0, album.UnsharpSigma);
            Assert.AreEqual(1.0, album.UnsharpThreshold);
            Assert.AreEqual(1, album.Watermark.Id);
            Assert.AreEqual(true, album.Watermarking);
            Assert.AreEqual(true, album.WorldSearchable);
            Assert.AreEqual(true, album.X2Larges);
            Assert.AreEqual(true, album.X3Larges);
            Assert.AreEqual(true, album.XLarges);

            // Write
            using (Mockery.Ordered)
            {
                this.ExpectOnce("ChangeAlbumBackprinting", "backprinting");
                this.ExpectOnce("ChangeAlbumCanRank", false);
                this.ExpectOnce("ChangeAlbumCategoryId", 1);
                this.ExpectOnce("ChangeAlbumClean", false);
                this.ExpectOnce("ChangeAlbumColorCorrection", 1);
                this.ExpectOnce("ChangeAlbumComments", false);
                this.ExpectOnce("ChangeAlbumCommunityId", 1);
                this.ExpectOnce("ChangeAlbumDefaultColor", false);
                this.ExpectOnce("ChangeAlbumDescription", "description");
                this.ExpectOnce("ChangeAlbumExif", false);
                this.ExpectOnce("ChangeAlbumExternal", false);
                this.ExpectOnce("ChangeAlbumFamilyEdit", false);
                this.ExpectOnce("ChangeAlbumFileNames", false);
                this.ExpectOnce("ChangeAlbumFriendEdit", false);
                this.ExpectOnce("ChangeAlbumGeography", false);
                this.ExpectOnce("ChangeAlbumHeader", false);
                //this.ExpectOnce("ChangeAlbumHidden", false);
                this.ExpectOnce("ChangeAlbumHideOwner", false);
                this.ExpectOnce("ChangeAlbumHighlightId", 1);
                //this.ExpectOnce("ChangeAlbumKeywords", "keywords");
                this.ExpectOnce("ChangeAlbumLarges", false);
                this.ExpectOnce("ChangeAlbumOriginals", false);
                this.ExpectOnce("ChangeAlbumPassword", "password");
                this.ExpectOnce("ChangeAlbumPasswordHint", "password hint");
                this.ExpectOnce("ChangeAlbumPosition", 1);
                this.ExpectOnce("ChangeAlbumPrintable", false);
                this.ExpectOnce("ChangeAlbumProofDays", 1);
                this.ExpectOnce("ChangeAlbumProtected", false);
                this.ExpectOnce("ChangeAlbumPublic", false);
                this.ExpectOnce("ChangeAlbumShare", false);
                this.ExpectOnce("ChangeAlbumSmugSearchable", false);
                this.ExpectOnce("ChangeAlbumSortDirection", false);
                this.ExpectOnce("ChangeAlbumSortMethod", "DateTime");
                this.ExpectOnce("ChangeAlbumSubCategoryId", 1);
                this.ExpectOnce("ChangeAlbumTemplateId", 1);
                this.ExpectOnce("ChangeAlbumTitle", "title");
                this.ExpectOnce("ChangeAlbumUnsharpAmount", 1.0f);
                this.ExpectOnce("ChangeAlbumUnsharpRadius", 1.0f);
                this.ExpectOnce("ChangeAlbumUnsharpSigma", 1.0f);
                this.ExpectOnce("ChangeAlbumUnsharpThreshold", 1.0f);
                this.ExpectOnce("ChangeAlbumWatermarkId", 1);
                this.ExpectOnce("ChangeAlbumWatermarking", false);
                this.ExpectOnce("ChangeAlbumWorldSearchable", false);
                this.ExpectOnce("ChangeAlbumX2Larges", false);
                this.ExpectOnce("ChangeAlbumX3Larges", false);
                this.ExpectOnce("ChangeAlbumXLarges", false);
            }

            album.Backprinting = "backprinting";
            album.CanRank = false;
            album.Category = categoryContract;
            album.Clean = false;
            album.ColorCorrection = 1;
            album.Comments = false;
            album.Community = communityContract;
            album.DefaultColor = false;
            album.Description = "description";
            album.Exif = false;
            album.External = false;
            album.FamilyEdit = false;
            album.FileNames = false;
            album.FriendEdit = false;
            album.Geography = false;
            album.Header = false;
            album.HideOwner = false;
            album.Highlight = highlightContract;
            album.Larges = false;
            album.Originals = false;
            album.Password = "password";
            album.PasswordHint = "password hint";
            album.Position = 1;
            album.Printable = false;
            album.ProofDays = 1;
            album.Protected = false;
            album.Public = false;
            album.Share = false;
            album.SmugSearchable = false;
            album.SortDirection = SortDirection.Descending;
            album.SortMethod = SortMethod.DateTime;
            album.SubCategory = subCategoryContract;
            album.Template = templateContract;
            album.Name = "title";
            album.UnsharpAmount = 1.0f;
            album.UnsharpRadius = 1.0f;
            album.UnsharpSigma = 1.0f;
            album.UnsharpThreshold = 1.0f;
            album.Watermark = watermarkContract;
            album.Watermarking = false;
            album.WorldSearchable = false;
            album.X2Larges = false;
            album.X3Larges = false;
            album.XLarges = false;
        }

        [Test]
        public void Mode()
        {
            AlbumContract albumContractEnabled = new AlbumContract()
            {
                CanRank = true,
                Clean = true,
                Comments = true,
                DefaultColor = true,
                Exif = true,
                External = true,
                FamilyEdit = true,
                FileNames = true,
                FriendEdit = true,
                Geography = true,
                Header = true,
                HideOwner = true,
                Larges = true,
                Originals = true,
                Printable = true,
                Protected = true,
                Public = true,
                Share = true,
                SmugSearchable = true,
                Watermarking = true,
                WorldSearchable = true,
                X2Larges = true,
                X3Larges = true,
                XLarges = true,
            };

            Album albumEnabled = new Album(
                albumContractEnabled,
                this.session,
                this.smugMugClient);

            Assert.AreEqual("gepPSsOcCFfs", albumEnabled.Mode);

            AlbumContract albumContractDisabled = new AlbumContract()
            {
                CanRank = false,
                Clean = false,
                Comments = false,
                DefaultColor = false,
                Exif = false,
                External = false,
                FamilyEdit = false,
                FileNames = false,
                FriendEdit = false,
                Geography = false,
                Header = false,
                HideOwner = false,
                Larges = false,
                Originals = false,
                Printable = false,
                Protected = false,
                Public = false,
                Share = false,
                SmugSearchable = false,
                Watermarking = false,
                WorldSearchable = false,
                X2Larges = false,
                X3Larges = false,
                XLarges = false,
            };

            Album albumDisabled = new Album(
                albumContractDisabled,
                this.session,
                this.smugMugClient);

            Assert.AreEqual("------------", albumDisabled.Mode);
        }
    }
}
